package com.freemyleft.left.zapp.wechat.templates.leftwechat;

import android.app.Activity;

import com.freemyleft.left.zapp.app.ConfigKeys;
import com.freemyleft.left.zapp.app.Left;
import com.freemyleft.left.zapp.wechat.templates.callbacks.IWeChatSignInCallback;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by FreeMyleft on 2017/12/15.
 */

public class LeftWeChat  {
   public static  final String APP_ID = Left.getConfiguration(ConfigKeys.WE_CHAT_APP_ID);
    public static  final String APP_SECRET=Left.getConfiguration(ConfigKeys.WE_CHAT_APP_SECRET);
    private final IWXAPI WXAPI;
    private IWeChatSignInCallback mSignInCallback=null;

    private static final class Holder{
        private static final LeftWeChat INSTANCE=new LeftWeChat();

    }
    public static LeftWeChat getInstance(){
        return Holder.INSTANCE;
    }
    private LeftWeChat(){
        final Activity activity=Left.getConfiguration(ConfigKeys.ACTIVITY);
        WXAPI= WXAPIFactory.createWXAPI(activity,APP_ID,true);
        WXAPI.registerApp(APP_ID);
    }
    /**
     * 向微信的客户端发请求登录
     */
    public IWXAPI getWXAPI(){
        return WXAPI;
    }
    public LeftWeChat onSignSuccess(IWeChatSignInCallback callback){
        this.mSignInCallback=callback;
        return this;
    }
       public IWeChatSignInCallback getmSignInCallback(){
           return mSignInCallback;
       }
    /**
     * 调用微信接口的方法
     */
    public final void signIn(){
        final SendAuth.Req req= new SendAuth.Req();
        req.scope="snsapi_userinfo";
        req.state="random_state";
        WXAPI.sendReq(req);
    }
}
